﻿@inherits LayoutComponentBase
@implements IDisposable
@layout BaseLayout
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
@inject IOptionsMonitor<WebsiteOptions> WebsiteOption
@inject IStringLocalizer<HomeLayout> Localizer

@Body

<footer>
    <div class="footer-body">
        <div>
            <h4>@Localizer["FooterH1"]</h4>
            <ul>
                <li>
                    <a class="footer-link" href="@WebsiteOption.CurrentValue.BootstrapAdminLink" target="_blank">Bootstrap Admin</a>
                </li>
                <li>
                    <a class="footer-link" href="https://gitee.com/Longbow/SliderCaptcha" target="_blank">@Localizer["FooterLi1"]</a>
                </li>
                <li>
                    <a class="footer-link" href="https://gitee.com/Longbow/longbow-select" target="_blank">Longbow-select</a>
                </li>
                <li>
                    <a class="footer-link" href="https://gitee.com/Longbow/longbow-validate" target="_blank">Longbow-validate</a>
                </li>
            </ul>
        </div>
        <div>
            <h4>@Localizer["FriendLink"]</h4>
            <ul>
                @foreach (var link in WebsiteOption.CurrentValue.Links)
                {
                    <li>
                        <a class="footer-link" href="@link.Value" target="_blank">@link.Key</a>
                    </li>
                }
            </ul>
        </div>
        <div>
            <h4>@Localizer["Community"]</h4>
            <ul>
                <li>
                    <div class="footer-link">@Localizer["CommunityLi2"]</div>
                </li>
                <li>
                    <div class="footer-link">@Localizer["CommunityLi1"]</div>
                </li>
                <li>
                    <div class="footer-link">@Localizer["CommunityLi3"]</div>
                </li>
            </ul>
        </div>
        <div>
            <h4>@Localizer["SpecialAcknowledgements"]</h4>
            <ul>
                <li>
                    <div class="footer-link">上海智通建设发展股份有限公司</div>
                </li>
                <li>
                    <div class="footer-link"><a href="http://ccflow.org/?from=blazor">@Localizer["FriendLi2"]</a></div>
                </li>
            </ul>
        </div>
    </div>
    <div class="footer-info d-none d-sm-flex">
        <div>Powered by .NET @Version on @OS</div>
        <div class="ms-1">BB @BBVersion</div>
        <div class="ms-2">Run @Runtime</div>
        <div class="d-flex flex-fill align-items-center justify-content-center">
            <a class="ms-3 d-none d-md-block" href="@WebsiteOption.CurrentValue.BootstrapBlazorLink" target="_blank">@Localizer["Footer"]</a>
            <div class="mx-3 d-none d-md-block">
                <img alt="12377@2x" class="footer-logo me-1" src="_content/BootstrapBlazor.Shared/images/12377@2x.png">
                <a href="https://12377.cn" target="_blank">违法和不良信息举报中心</a>
            </div>
            <div class="me-3">
                <a href="https://beian.miit.gov.cn/" target="_blank">鲁ICP备19015061号</a>
            </div>
        </div>
        <div class="d-flex">
            <img alt="global" class="footer-lang" src="_content/BootstrapBlazor.Shared/images/global.svg" />
            <a @onclick:preventDefault @onclick="@(e => SetLang("zh-CN"))">简 体</a>
            <span class="mx-1">/</span>
            <a @onclick:preventDefault @onclick="@(e => SetLang("en-US"))">English</a>
        </div>
    </div>
</footer>

@code {
    private string Version => Environment.Version.ToString();

    [NotNull]
    private string? OS { get; set; }

    [CascadingParameter(Name = "BBVersion")]
    private string? BBVersion { get; set; }

    [Inject]
    [NotNull]
    private ICacheManager? Cache { get; set; }

    private string? Runtime { get; set; }

    private string SelectedCulture { get; set; } = CultureInfo.CurrentUICulture.Name;

    private CancellationTokenSource DisposeTokenSource { get; } = new();

    protected override void OnInitialized()
    {
        if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
        {
            OS = "Windows";
        }
        else if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
        {
            OS = "OSX";
        }
        else if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
        {
            OS = "Linux";
        }
        else
        {
            OS = "Unknown";
        }

        UpdateRuntime();
    }

    /// <summary>
    /// OnAfterRenderAsync 方法
    /// </summary>
    /// <param name="firstRender"></param>
    /// <returns></returns>
    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            _ = Task.Run(async () =>
            {
                while (!DisposeTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        await Task.Delay(1000, DisposeTokenSource.Token);
                    }
                    catch (TaskCanceledException)
                    {

                    }
                    if (!DisposeTokenSource.IsCancellationRequested)
                    {
                        UpdateRuntime();
                        await InvokeAsync(StateHasChanged);
                    }
                }
            });
        }
    }

    private void UpdateRuntime()
    {
        var ts = DateTimeOffset.Now - Cache.GetStartTime();
        Runtime = ts.ToString("dd\\.hh\\:mm\\:ss");
    }

    private async Task SetLang(string cultureName)
    {
        if (OperatingSystem.IsBrowser())
        {
            if (cultureName != CultureInfo.CurrentCulture.Name)
            {
                await JSRuntime.InvokeVoidAsync(identifier: "$.blazorCulture.set", cultureName);
                var culture = new CultureInfo(cultureName);
                CultureInfo.CurrentCulture = culture;
                CultureInfo.CurrentUICulture = culture;

                NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
            }
        }
        else
        {
            // 使用 api 方式 适用于 Server-Side 模式
            if (SelectedCulture != cultureName)
            {
                var culture = cultureName;
                var uri = new Uri(NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
                var query = $"?culture={Uri.EscapeDataString(culture)}&redirectUri={Uri.EscapeDataString(uri)}";

                // use a path that matches your culture redirect controller from the previous steps
                NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
            }
        }
    }

    private void Dispose(bool disposing)
    {
        if (disposing)
        {
            DisposeTokenSource.Cancel();
            DisposeTokenSource.Dispose();
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
